package org.thingsboard.server.dao.sql.schedulerEvent;


import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.thingsboard.server.common.data.SchedulerEventInfo;
import org.thingsboard.server.common.data.SchedulerEventStatus;
import org.thingsboard.server.dao.model.sql.DashboardInfoEntity;
import org.thingsboard.server.dao.model.sql.SchedulerEventEntity;
import org.thingsboard.server.dao.model.sql.SchedulerEventInfoEntity;

import java.util.List;
import java.util.UUID;

import java.util.UUID;

public interface SchedulerEventInfoRepository extends CrudRepository<SchedulerEventInfoEntity, UUID> {

    Long countByTenantId(UUID tenantId);

    @Query("SELECT di FROM SchedulerEventInfoEntity di WHERE di.tenantId = :tenantId " +
            "AND LOWER(di.type) LIKE LOWER(CONCAT(:searchText, '%'))")
    Page<SchedulerEventInfoEntity> findByTenantId(@Param("tenantId")UUID tenantId,  @Param("searchText") String searchText, Pageable toPageable);



    @Query("SELECT di FROM SchedulerEventInfoEntity di, RelationEntity re WHERE di.tenantId = :tenantId " +
            "AND di.id = re.toId AND re.toType = 'SCHEDULER' AND re.relationTypeGroup = 'SCHEDULER' " +
            "AND re.relationType = 'Contains' AND re.fromId = :customerId AND re.fromType = 'CUSTOMER' " +
            "AND LOWER(di.type) LIKE LOWER(CONCAT(:searchText, '%'))")
    Page<SchedulerEventInfoEntity>  findByTenantIdAndCustomerId(@Param("tenantId") UUID tenantId,
                                                                @Param("customerId") UUID customerId,
                                                                @Param("searchText") String searchText, Pageable toPageable);

    @Query("SELECT di FROM SchedulerEventInfoEntity di WHERE di.status = :status")
    Page<SchedulerEventInfoEntity>  findSchedulerEventInfosByStatus( @Param("status")SchedulerEventStatus status, Pageable toPageable);
}

